<?php
namespace bee;

function processPage()
{
  $cfg = config::getInstance();
  $_SESSION["sys_page"] = $_REQUEST["sys_page"];
  if(owns_acces(app::getInstance()->getUser()->id, @$_REQUEST["sys_page"], isset($_REQUEST["sys_action"]) ? $_REQUEST["sys_action"] : ""))
  {
    ;
  } else
  { processor::getInstance()->denyUser();//denyUser();
  }
  $DSDoc = XMLDataHub::getInstance();
  $styleSheet = new   \DOMDocument();
  $DSDoc->setTableFromArr(processor::getInstance()->getUserJobs(), "sys_permisions");
  $processor = new    \XSLTProcessor();
  $style = new   \DOMDocument();
  $styleSheet->load($cfg->sys_TEMPLATE_PATH . "/main.xsl");
  $main = $styleSheet->getElementsByTagName("call-template");
  foreach ($main as $child)
  {
    if($child->getAttribute("name") == "zamijeni-me-php")
    {
      $child->setAttribute("name", str_replace("/", "-", $_REQUEST["sys_page"]));
    }
  }
  $main = $styleSheet->getElementsByTagName("include");
  foreach ($main as $child)
  {
    if($child->getAttribute("href") == "zamijeni-me-php")
    {
      $child->setAttribute("href", $_REQUEST["sys_page"] . ".xsl");
    }
  }
  /**
   *
   * @todo duplicirano u __form
   */
  //echo "----------";
  $webMeni = new webMeni();
  $rs = new rowSet($webMeni);
  $arr_meni = array();
  while($rs->getRow($webMeni))
  {
    if(owns_acces(app::getInstance()->getUser()->id, $webMeni->command))
    {
      $arr_meni[] = clone $webMeni;
    }
  }
  $DSDoc->setTableFromArr($arr_meni, "webMeni");

  $state = new state(1, filter_var(isset($_REQUEST["sys_stateID"]) ? $_REQUEST["sys_stateID"] : "", FILTER_VALIDATE_INT));
  { if(@$state->tmp->session && @$state->tmp->session != session_id())
  {
    processor::denyUser();
  }
  }
  $state->setField("sys_page", $_REQUEST["sys_page"]);
  @$state->tmp->session =  session_id();
  $state->tmp->expire = date("Y-m-d H:i:s", time() + 60 * 60);
  $DSDoc->setVariable(  "app_TIME",         date( "d.m.Y H:i:s"));
  $DSDoc->setVariable(  "app_USERNAME",     app::getInstance()->getUser()->login);
  $DSDoc->setVariable(  "sys_generate_XML", $cfg->sys_generate_XML);
  $DSDoc->setVariable(  "sys_page",         $_REQUEST["sys_page"]);
  $DSDoc->setVariable(  "sys_action",       isset($_REQUEST["sys_action"]) ? $_REQUEST["sys_action"] : "");
  $DSDoc->setVariable(  "sys_kom_filter",   isset($kom_filter) ? $kom_filter : "");
  $DSDoc->setVariable(  "sys_CSS_STYLE",    $cfg->sys_CSS_STYLE);

  require  config::getInstance()->sys_APP_PATH . "/usr_app/" . $_REQUEST["sys_page"] . ".php";
  //  die("123");
  if(@$_REQUEST["app_back"])
  {
    $state->setField("app_back", @$_REQUEST["app_back"]);
  }
  $sys_stateID = $state->save();
  /**
   * @todo dosta se funkcionalnosti "duplicira" sa processForm()
  */
  if(isset($_REQUEST["__frm_back"]))
  {
    $DSDoc->setVariable("__frm_back", $_REQUEST["__frm_back"]);
  }

  $DSDoc->setVariable(  "sys_php_last_time", date("ymdhi", filemtime($cfg->sys_USR_APP_PATH . "/" . $_REQUEST["sys_page"] . ".php")));
  $DSDoc->setVariable(  "sys_frm_last_time", date("ymdhi", filemtime($cfg->sys_TEMPLATE_PATH ."/" . $_REQUEST["sys_page"] . ".xsl")));
  $DSDoc->setVariable(  "app_HEADLINE",     isset($app_HEADLINE) ? $app_HEADLINE : "" );
  $DSDoc->setVariable(  "sys_frm_version",  isset($sys_frm_version) ? $sys_frm_version : "");
  $DSDoc->setVariable(  "sys_msg_style",    isset(app::getInstance()->getMessageLevel) ? app::getInstance()->getMessageLevel : @$sys_msg_style);
  $DSDoc->setVariable(  "sys_msg",          (isset($_REQUEST["sys_msg"]) ? ($_REQUEST["sys_msg"] . "\n") : "") . app::getInstance()->getMessageText());
  if(isset($sys_stateID))
  {
    $DSDoc->setVariable("sys_stateID",      $sys_stateID);
  }
  if($state->getField("app_back"))
  {
    $DSDoc->setVariable(  "app_back",       $state->getField("app_back"));
    $state_back =             new state(1,  $state->getField("app_back"));
    $DSDoc->setVariable("app_back_page",    $state_back->getField("sys_page"));
  }
  if($cfg->sys_IS_DEBUG_MODE)
  {
    $DSDoc->setVariable("sys_DEBUG_INFO", isset($echo)? $echo : "");
  }
  //print_r($styleSheet);
  $processor->importStylesheet($styleSheet);
  $processor->setParameter("", "sys_page", isset($sys_menu_page) ? $sys_menu_page : $_REQUEST["sys_page"]);
  $processor->setParameter("", "__form", isset(app::$activeMenu) ? app::$activeMenu : @$_REQUEST["__form"]);
  $processor->setParameter("", "sys_action", isset($_REQUEST["sys_action"]) ? $_REQUEST["sys_action"] : "");

  echo $processor->transformToXml($DSDoc);
  if(isset($_REQUEST["sys_page"]) && $cfg->sys_generate_XML)
  {
    @$DSDoc->save($cfg->sys_TMP_PATH. "/" . $_REQUEST["sys_page"] . ".xml" );//****
  }
}
